<?php
namespace Home\Controller;
use Think\Controller;
class ArticleController extends Controller {
    //前置操作方法
    public function _before_index(){
        $user = is_login();
        if(!$user){
            $this->redirect('Login/index');
        }
    }
    /**
     * 新闻动态
     */
    public function index() {
        $M_article          = M('article');
        $M_article_sort     = M('article_sort');
        $pagesize	        = '1';
        $sort = $M_article_sort->select();
        $this->assign('sort',$sort);
        if(IS_POST){
            if(I('post.title')){
                $title = I('post.title');
                $where['title'] = array('like', "%$title%");
            }
            $where['cateid']  = I('post.cateid');
            $page = I('post.page');
            $start = ($page-1)*$pagesize;
            $list      = $M_article->where($where)->limit($start,$pagesize)->order('addtime desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['addtime'] = date('Y/m/d',strtotime($v['addtime']));
            }
            if($list){
                $return_data     = array('code' => 0,'data' => $list,'page'=>$page+1);
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '没有更多的新闻了');
                $this->ajaxReturn($return_data,'json');
            }
        }
        $this->assign(array(
            'cateid'    => $where['cateid'],
            'sort'      => $sort,
        ));
        $this->display();
    }
    /**
     * 新闻标题搜索
     */
    public function title_search(){
        $title      = I('get.title');
        $cateid     = I('get.cateid');
        $M_article  = M('article');
        $where['title']   = array('like', "%$title%");
        $where['cateid']  = $cateid;
        $list = $M_article->where($where)->select();
        if($list){
            $return     = array('code'=>0,'data'=>$list);
            $this->ajaxReturn($return,'json');
        }else{
            $return     = array('code'=>-1,'msg'=>'没有找到相关新闻！');
            $this->ajaxReturn($return,'json');
        }
    }
    /**
     * 新闻详情
     */
    public function articledetail(){
        $id         = I('get.id');
        $M_article  = M('article');
        $list = $M_article ->find($id);
        $this->assign('list',$list);
        $this->display();
    }
    /**
     * 公告列表
     */
    public function noticelist(){
        $M_notice   = M('notice');
        $pagesize	= '1';
        if(IS_POST){
            $page   = I('post.page');
            $start  = ($page-1)*$pagesize;
            $list   = $M_notice ->limit($start,$pagesize)->order('addtime desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['addtime'] = date('Y/m/d',strtotime($v['addtime']));
            }
            //echo $M_notice->getLastSql();die;
            if($list){
                $return_data     = array('code' => 0,'data' => $list,'page'=>$page+1);
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '没有更多的公告了');
                $this->ajaxReturn($return_data,'json');
            }
        }
        $this->display();
    }
    /**
     * 公告详情
     */
    public function noticedetail(){
        $id         = I('get.id');
        $M_notice   = M('notice');
        $M_notice_b = M('notice_browse');
        $uid        = cookie('uid');
        $where['uid']= $uid;
        $info = $M_notice_b->where($where)->find();
        /*if($info){
            $nid_arr = json_decode($info['nid'],true);
            if(!in_array($id,$nid_arr)){
                $nid_arr[]      = $id;
                $data['nid']    = json_encode($nid_arr);
                $data['num']    = $info['num'] + 1;
                $M_notice_b->where('uid = '.$uid)->save($data);
            }
        }else{
            $arr[]          = $id;
            $data['uid']    = $uid;
            $data['nid']    = json_encode($arr);
            $data['num']    = 1;
            $data['addtime'] = date('Y-m-d H:i:s',time());
            $M_notice_b->add($data);
        }*/
        $info = $M_notice->find($id);
        $this->assign('info',$info);
        $this->display();
    }
    /**
     * 继续教育列表
     */
    public function learnlist() {
        $M_learn        = M('learn');
        $M_learn_sort   = M('learn_sort');
        $sort           = $M_learn_sort->select();
        $pagesize	    = '1';
        $this->assign('sort',$sort);
        if(IS_POST){
            if(I('post.title')){
                $title = I('post.title');
                $where['title'] = array('like', "%$title%");
            }
            $where['cateid']  = I('post.cateid');
            $page = I('post.page');
            $start = ($page-1)*$pagesize;
            $list      = $M_learn->where($where)->limit($start,$pagesize)->order('addtime desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['addtime'] = date('Y/m/d',strtotime($v['addtime']));
            }
            //echo $M_learn->getlastsql();
            if($list){
                $return_data     = array('code' => 0,'data' => $list,'page'=>$page+1);
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '没有更多内容了');
                $this->ajaxReturn($return_data,'json');
            }
        }

        $this->display();
    }
}